package com.dqhl.community.adapter;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dqhl.community.R;
import com.dqhl.community.model.ShopCartBean;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.Dlog;

import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2016/9/19.
 * 购物车adapter
 */
public class ShopCartAdapter extends BaseAdapter {

    private Handler mHandler;
    private Context context;            //上下文对象
    private List<ShopCartBean> list_sc; //数据集合List
    private LayoutInflater inflater;    //布局填充器
    private static HashMap<Integer, Boolean> isSelected;
    private static HashMap<Integer, Integer> numbers;
    int num;// 商品数量

    public ShopCartAdapter(Context context, List<ShopCartBean> list, Handler mHandler) {
        this.context = context;
        this.list_sc = list;
        this.inflater = LayoutInflater.from(context);
        this.mHandler = mHandler;
        isSelected = new HashMap<Integer, Boolean>();
        numbers = new HashMap<Integer, Integer>();
        initData();
    }

    // 初始化isSelected的数据
    private void initData() {
        for (int i = 0; i < list_sc.size(); i++) {
            getIsSelected().put(i, false);
            getNumbers().put(i, list_sc.get(i).getNumber());
        }
        Dlog.e("numbers:" + numbers.toString());
    }

    @Override
    public int getCount() {
        return list_sc.size();
    }

    @Override
    public Object getItem(int i) {
        return list_sc.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolders vhs = null;
//        ShopCartBean shopCartBean = list_sc.get(position);
        if (convertView == null) {
            vhs = new ViewHolders();
            convertView = inflater.inflate(R.layout.layout_shopcart_item, null);
            vhs.ck_select = (CheckBox) convertView.findViewById(R.id.ck_select);
            vhs.iv_pic_goods = (ImageView) convertView.findViewById(R.id.pic_goods);
            vhs.tv_id_goods = (TextView) convertView.findViewById(R.id.tv_id_goods);
            vhs.tv_price_goods = (TextView) convertView.findViewById(R.id.tv_price_goods);
            vhs.tv_goods_count = (TextView) convertView.findViewById(R.id.tv_number);
            vhs.btn_minus = (Button) convertView.findViewById(R.id.btn_minus);
            vhs.btn_plus = (Button) convertView.findViewById(R.id.btn_plus);
            convertView.setTag(vhs);
        } else {
            vhs = (ViewHolders) convertView.getTag();
        }

        vhs.ck_select.setChecked(getIsSelected().get(position));
        vhs.tv_goods_count.setText(getNumbers().get(position).toString());
        if (getIsSelected().get(position)) {
            vhs.ck_select.setChecked(true);
        } else {
            vhs.ck_select.setChecked(false);
        }

        String a = vhs.tv_goods_count.getText().toString();
        Dlog.e("11111111:" + a);
        list_sc.get(position).setNumber(Integer.parseInt(a));

        ShopCartBean test = list_sc.get(position);
        vhs.tv_id_goods.setText(test.getName());
        vhs.tv_price_goods.setText(test.getPrice());
        vhs.tv_goods_count.setText(test.getNumber() + "");
        x.image().bind(vhs.iv_pic_goods, Config.Api.base_img_url + test.getPic_a());

        init(vhs, position);

        return convertView;
    }

    private void init(final ViewHolders vhs, final int position) {
        vhs.ck_select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isSelected.put(position, true);
                getIsSelected().put(position, isChecked);
                vhs.ck_select.setChecked(getIsSelected().get(position));
                mHandler.sendMessage(mHandler.obtainMessage(10, getTotalPrice()));

                Iterator iterator = isSelected.entrySet().iterator();
                List<Boolean> array = new ArrayList<Boolean>();
                while (iterator.hasNext()) {
                    HashMap.Entry entry = (HashMap.Entry) iterator.next();
                    Integer key = (Integer) entry.getKey();
                    Boolean val = (Boolean) entry.getValue();
                    array.add(val);
                }
                mHandler.sendMessage(mHandler.obtainMessage(11, array));
            }
        });

        final String numString = vhs.tv_goods_count.getText().toString();
        Dlog.e("numString::::::" + numString);
        vhs.btn_plus.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Dlog.e("numString:" + numString);
                if (numString == null || numString.equals("")) {
                    num = 1;
                    vhs.tv_goods_count.setText("1");
                    Dlog.e("------::" + num);
                } else {
                    num = Integer.parseInt(vhs.tv_goods_count.getText().toString());
                    Dlog.e(num + "");
                    num++;
                    vhs.tv_goods_count.setText(String.valueOf(num));
                    list_sc.get(position).setNumber(num);
                    numbers.put(position, num);
                    Dlog.e("+:" + num);
                    mHandler.sendMessage(mHandler.obtainMessage(10, getTotalPrice()));
                }
            }
        });
        vhs.btn_minus.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                num = Integer.parseInt(numString);
                if (numString == null || numString.equals("")) {
                    num = 1;
                    vhs.tv_goods_count.setText("1");
                } else {
                    num = Integer.parseInt(vhs.tv_goods_count.getText().toString());
                    Dlog.e(num + "");
                    if (num > 1) {
                        num--;
                        vhs.tv_goods_count.setText(String.valueOf(num));
                        Dlog.e("-:" + num);
                        list_sc.get(position).setNumber(num);
                        numbers.put(position, num);
                        mHandler.sendMessage(mHandler.obtainMessage(10, getTotalPrice()));
                    } else {
                        Toast.makeText(context, "数量不能为0", Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });

    }

    private double getTotalPrice() {
        ShopCartBean bean = null;
        double totalPrice = 0;
        for (int i = 0; i < list_sc.size(); i++) {
            bean = list_sc.get(i);
            if (getIsSelected().get(i)) {
                totalPrice += bean.getNumber() * Double.valueOf(bean.getPrice());
            }
        }
        return totalPrice;
    }

    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        ShopCartAdapter.isSelected = isSelected;
    }

    public static HashMap<Integer, Integer> getNumbers() {
        return numbers;
    }

    public static void setNumbers(HashMap<Integer, Integer> numbers) {
        ShopCartAdapter.numbers = numbers;
    }

    static class ViewHolders {
        public CheckBox ck_select;
        public ImageView iv_pic_goods;
        public TextView tv_id_goods;
        public TextView tv_price_goods;
        public TextView tv_goods_count;
        public Button btn_minus;
        public Button btn_plus;
    }
}
